using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz.Report;
using EntLibEC.Biz;

namespace EntLibEC.View.Report
{
	/// <summary>
	/// Summary description for FinanceCostDistribute.
	/// </summary>
	public partial class FinanceCostDistribute : PageBase
	{

		private decimal normalCostSum = 0m;
		private decimal secondCostSum = 0m;
		private decimal badCostSum = 0m;
		private decimal totalCostSum = 0m;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			base.CheckAccess((int)AppEnum.Privilege.FinanceReportSearch,true ,null);
			if ( !Page.IsPostBack)
				btnExport.Enabled = false;
		}

		private bool checkForm()
		{
			return true;
		}
		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			if ( !checkForm())
				return;

			DataSet ds = ReportManager.GetInstance().GetCostDistributeDs();
			DataGrid1.DataSource = ConvertDs(ds);
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
			{
				rows = ds.Tables[0].Rows.Count;
				btnExport.Enabled = true;
			}
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return null;

			ds.Tables[0].Columns.Add("NormalCostCvt");
			ds.Tables[0].Columns.Add("SecondCostCvt");
			ds.Tables[0].Columns.Add("BadCostCvt");
			ds.Tables[0].Columns.Add("TotalCostCvt");
			
			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				decimal normalcost = Util.TrimDecimalNull(dr["normalcost"]);
				decimal secondcost = Util.TrimDecimalNull(dr["secondcost"]);
				decimal badcost = Util.TrimDecimalNull(dr["badcost"]);
				decimal totalcost = Util.TrimDecimalNull(dr["totalcost"]);

				dr["NormalCostCvt"] = (normalcost/1.17m).ToString(AppConst.DecimalFormatWithGroup);
				dr["SecondCostCvt"] = (secondcost/1.17m).ToString(AppConst.DecimalFormatWithGroup);
				dr["BadCostCvt"] = (badcost/1.17m).ToString(AppConst.DecimalFormatWithGroup);
				dr["TotalCostCvt"] = (totalcost/1.17m).ToString(AppConst.DecimalFormatWithGroup);

				normalCostSum += normalcost;
				secondCostSum += secondcost;
				badCostSum += badcost;
				totalCostSum += totalcost;
			}
			return ds;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

		}
		#endregion

		private string ToFooterString(int x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.IntFormatWithGroup);
		}
		private string ToFooterString(decimal x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.DecimalFormatWithGroup);
		}

		protected void btnExport_Click(object sender, System.EventArgs e)
		{
			Util.ToExcel(DataGrid1, "Inventory Report", "");
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Footer)
			{
				e.Item.Cells[1].Text = ToFooterString(normalCostSum/1.17m);
				e.Item.Cells[2].Text = ToFooterString(secondCostSum/1.17m);
				e.Item.Cells[3].Text = ToFooterString(badCostSum/1.17m);
				e.Item.Cells[4].Text = ToFooterString(totalCostSum/1.17m);
			}
		}
	}
}
